//
//  Account_iPhone_ViewController.m
//  chilemat
//
//  Created by Cristian Gonzalez Figueroa on 26-03-14.
//  Copyright (c) 2014 Chilemat SpA. All rights reserved.
//

#import "Account_iPad_ViewController.h"
#import "dao.h"
#import <FacebookSDK/FacebookSDK.h>

@interface Account_iPad_ViewController ()

@end

@implementation Account_iPad_ViewController

@synthesize btnLogout, imgViewCloseSession, lblCloseSession, viewLineCloseSession;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"ipad_header_portrait.png"] forBarMetrics:UIBarMetricsDefault];
    
    //  self.view.hidden = YES;
    
    UIView *viewHiddenPage = [[UIView alloc] init];
    
    [viewHiddenPage setFrame:CGRectMake(0, 0, 768, 900)];
    [viewHiddenPage setBackgroundColor:[UIColor whiteColor]];
    
    [self.view addSubview:viewHiddenPage];
    
    // ¿Está registrado?
    dao *objDAO = [[dao alloc] init];
    
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    int intIdUsrRegistered = 0;
    // ¿Está registrado?
    intIdUsrRegistered = [objDAO obtainUsrRegistered:intIdUsuario];

    int intUsrLoggedIn = [objDAO obtainUsrLoggedIn:intIdUsuario];
    
    if ((intIdUsrRegistered == -1) && (intUsrLoggedIn == -1)) { //No aparece como registrado ni logeado por algun proceso
        
        //Utilizado para cambiar el flujo de manera posterior segun desde donde se solicita el Login
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        [prefs setObject:@"2" forKey:@"strCurrentTypeOfLogin"]; //1: Login normal, 2: Login desde el área personal
        
        [self performSegueWithIdentifier:@"segueInitLogin" sender:nil];
    }else{
        
        [viewHiddenPage setFrame:CGRectMake(0, 0, 0, 0)];
        viewHiddenPage.hidden = YES;
        
        int intUsrLoggedEmail = [objDAO obtainUsrLogged:5 idUsuario:intIdUsuario];
        int intUsrLoggedFacebook = [objDAO obtainUsrLogged:1 idUsuario:intIdUsuario];
        
        if ((intUsrLoggedEmail == -1) && (intUsrLoggedFacebook == -1)) {
            
            btnLogout.hidden = YES;
            
            imgViewCloseSession.hidden = YES;
            lblCloseSession.hidden = YES;
            viewLineCloseSession.hidden = YES;
            
        }else{
            btnLogout.hidden = NO;
            
            imgViewCloseSession.hidden = NO;
            lblCloseSession.hidden = NO;
            viewLineCloseSession.hidden = NO;
        }
    }
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"ipad_header_portrait.png"] forBarMetrics:UIBarMetricsDefault];

    // ¿Está registrado?
    dao *objDAO = [[dao alloc] init];
    
    int intIdUsuario = [objDAO obtainCurrentIdUser];
    
    int intUsrLoggedEmail = [objDAO obtainUsrLogged:5 idUsuario:intIdUsuario];
    int intUsrLoggedFacebook = [objDAO obtainUsrLogged:1 idUsuario:intIdUsuario];
    
    if ((intUsrLoggedEmail == -1) && (intUsrLoggedFacebook == -1)) {
        
        btnLogout.hidden = YES;
        
        imgViewCloseSession.hidden = YES;
        lblCloseSession.hidden = YES;
        viewLineCloseSession.hidden = YES;
        
    }else{
        btnLogout.hidden = NO;
        
        imgViewCloseSession.hidden = NO;
        lblCloseSession.hidden = NO;
        viewLineCloseSession.hidden = NO;
    }
}
*/


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (IBAction)callNumber:(id)sender {
    
    NSString *strContactPhone;
    
    dao *objDAO = [[dao alloc] init];
    
    strContactPhone = [NSString stringWithFormat:@"tel://%@", [objDAO obtainCallCenterNumber]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:strContactPhone]];
}

- (IBAction)logout:(id)sender {
    
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @""
                                                   message: @"¿Desea cerrar sesión en el sistema?"
                                                  delegate: self
                                         cancelButtonTitle:@"Cancelar"
                                         otherButtonTitles:@"Cerrar Sesión",nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex != 0) //Cerrar sesión
    {
        NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
        
        [prefs setObject:@"1" forKey:@"strCurrentTypeOfLogin"]; //1: Login normal, 2: Login desde el área personal
        
        // ¿Está registrado?
        dao *objDAO = [[dao alloc] init];
        
        int intIdUsuario = [objDAO obtainCurrentIdUser];
        
        int intUsrLoggedFacebook = [objDAO obtainUsrLogged:1 idUsuario:intIdUsuario];
        
        if (intUsrLoggedFacebook != -1) {
/*
            if (FBSession.activeSession.isOpen)
            {
                [FBSession.activeSession closeAndClearTokenInformation];
            }
*/
            [FBSession.activeSession closeAndClearTokenInformation];
            
            
        }
        
        //Esto aplica al login por correo además (la posibilidad de hacer logout).
        
        [objDAO deleteUsrLogged:intIdUsuario];
        [objDAO deleteUsrRegistered:intIdUsuario];
        [objDAO deleteCurrentIdUser];
        
        btnLogout.hidden = YES;
        imgViewCloseSession.hidden = YES;
        lblCloseSession.hidden = YES;
        viewLineCloseSession.hidden = YES;
        
        [self.tabBarController setSelectedIndex:0];
        
    }else{
        
        NSLog(@"Nothing");
    }
    
}


@end

